package com.cloudsigns.factorymanager2.engineering.model;

        import android.text.TextUtils;

        import com.cloudsigns.factorymanager2.MyApplicaton;
        import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
        import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
        import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
        import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
        import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

        import org.apache.http.HttpResponse;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.impl.client.DefaultHttpClient;
        import org.apache.http.protocol.HTTP;
        import org.apache.http.util.EntityUtils;
        import org.json.JSONArray;
        import org.json.JSONObject;

        import java.util.ArrayList;
        import java.util.List;

public class CompletionOrderListForMapModel implements ICompletionOrderListForMapModel {

    @Override
    public void getCompletionOrderListForMap(final OnCompletionOrderListForMapListener cmpletionOrderListForMapListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {


                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(ProjectUtil.COMPLETION_ORDER_LIST_FOR_MAP_URL);

                HttpResponse response;

                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");

                    if (null != cmpletionOrderListForMapListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("data"));
                            List<OrderParentBean> allOrderList = new ArrayList<OrderParentBean>();

                            if (jsonArr != null) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        OrderParentBean orderBean = new OrderParentBean();

                                        orderBean.setEngineeringAddress(jsonObj.getString("engineeringAddress"));
                                        orderBean.setId(jsonObj.getString("orderId"));

                                        orderBean.setEngineeringName(jsonObj.getString("engineeringName"));
                                        orderBean.setProjectName(jsonObj.getString("projectName"));
                                        orderBean.setItemId(jsonObj.getString("itemId"));

                                        String latStr = jsonObj.getString("lat");
                                        if (!TextUtils.isEmpty(latStr)) {
                                            orderBean.setLat(Double.parseDouble(latStr));
                                        }

                                        String lonStr = jsonObj.getString("lon");
                                        if (!TextUtils.isEmpty(lonStr)) {
                                            orderBean.setLon(Double.parseDouble(lonStr));
                                        }


                                        allOrderList.add(orderBean);


                                    }
                                }
                            }


                            cmpletionOrderListForMapListener.getCompletionOrderListForMapSuccessed(allOrderList);
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }

                            cmpletionOrderListForMapListener.getCompletionOrderListForMapFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnCompletionOrderListForMapListener {
        void getCompletionOrderListForMapSuccessed(List<OrderParentBean> allOrderList);

        void getCompletionOrderListForMapFailed(String desc);
    }
}
